package com.integration.commons;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class PropertyHolder {
	public static final String CONFIG_FILE_NAME = "config.properties";
	public static final String DEFAULT_CONFIG_FILE_NAME = "META-INF//config.properties";
	private static PropertyHolder instance;
	
	private Properties props;
	
	private PropertyHolder() {
		props = new Properties();
		try {
			props.load(new FileReader(CONFIG_FILE_NAME));
		} catch (FileNotFoundException e) {
			System.out.println("External config file not found. Trying default.");
			tryLoadDefaultConfigFile();
		} catch (IOException e) {
			System.out.println("Error reading config file. Trying default.");
			tryLoadDefaultConfigFile();
			e.printStackTrace();
		}
	}

	private void tryLoadDefaultConfigFile() {
		try {
			props.load(new FileReader(DEFAULT_CONFIG_FILE_NAME));
		} catch (FileNotFoundException e1) {
			System.out.println("Default config file not found.");
			e1.printStackTrace();
		} catch (IOException e1) {
			System.out.println("Error reading Default config file.");
			e1.printStackTrace();
		}
	}
	
	public static PropertyHolder getInstance(){
		if (instance == null){
			instance = new PropertyHolder();
		}
		return instance;
	}
	
	public String getProperty(String key){
		return props.getProperty(key);
	}
	
}
